<?php
$device_obj = new device();
$project_obj = new project();

$action_ary = array(
	'improt_time_out' => 'device_act_time_out',
);


/**
 * 导出装车计时登记记录信息
 * @author leiqianyong 2020-3-23 14:34 PM
 */
function device_act_time_out(){
    global $device_obj;
    $data = $_GET;

    $list=$device_obj->get_act_time_report($data);
    if(empty($list))exit('No Data !');

    //按设备组合数据，用于格式化表格
    $group=0; //设备分组编号
    $tag_dev='';//设备编号
    $group_posi=0;//记录在组中的位置
    $time_start=0; //记录开始时间
    $time_end=0; //记录截止时间
    $group_num=0; //分组中记录数量
    foreach ($list as $key=>$vo){
        //如果设备号发生变化，变更对应参数
        if($vo['did']!=$tag_dev){
            $group=0;
            $group_posi=0;
            $group_num=0;
            $tag_dev=$vo['did'];
        }

        //分组中第一个记录
        if(0==$group_posi){
            $group_num++;
            $time_end=$time_start=$vo['ctime'];
            $vo['maxtime']=$vo['mintime']=$vo['ctime'];
            $vo['num']=$group_num;
            $group_posi++;
        }else{
            //间隔时间超过5分钟，重新分组
            if(($vo['ctime']-$time_end)>300){
                $group++;
                $group_num=1;
                $group_posi=1;
                $vo['num']=$group_num;
                $time_end=$time_start=$vo['ctime'];
                $vo['maxtime']=$vo['mintime']=$vo['ctime'];
            }else{
                $group_num++;
                $vo['num']=$group_num;
                $time_end=$vo['ctime'];
                $vo['mintime']=$time_start;
                $vo['maxtime']=$vo['ctime'];
                $group_posi++;
            }
        }

        $res[$tag_dev][$group]=$vo;
    }


    //剔除一条记录
    foreach ($res as $key=>$vo){
        $rtn=false;
        foreach($vo as $k=>$v)if($v['num']>1)$rtn[]=$v;

        if($rtn)$result[$key]=$rtn;
    }

    if(empty($result))exit('No Data !');

    $title_ext = '辅助工作计时统计签认表' ;
    $filename = "SYT_REPORT:".date('YmdHis') ;  //ⅠⅡⅢ

    $title = array('序号','名称','产权人','次数','开始时间','结束时间','时间','备注','签认');
    $cols_num = count($title);
    $html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';

    $html .= '<tr><td width="100" colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';

    //导出xls 开始
    if (!empty($title)){
        $html .= '<tr>' ;
        foreach ($title as $k => $v) {
            $html .= '<td width="100" align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
        }
        $html .= '</tr>' ;
    }


    $keynum=0;
    foreach($result as $vo){
        $keynum++;
        $num=count($vo)+1;
        $time_start=''; //开始时间
        $time_per_sum=0; //总时间
        $k=0;
        foreach($vo as $cv){
            $time_sum = $cv['maxtime']-$cv['mintime'];
            $time_per_sum+=$time_sum;

            if(0==$k){
                $html .= '<tr>' ;
                $html .= '<td width="100" rowspan="'.$num.'" align=center x:num>'.$keynum.'</td>' ;
                $html .= '<td width="100" rowspan="'.$num.'"  align=left x:str><strong>'.$cv['dname'].'</strong></td>' ;
                $html .= '<td width="100" rowspan="'.$num.'"  align=left x:str>'.$cv['downer'].'</td>' ;
                $html .= '<td width="100" align=center x:str>'.($k+1).'</td>' ;
                $html .= '<td width="200" align=right x:num>'.date('Y-m-d H:i',$cv['mintime']).'</td>';
                $html .= '<td width="200" align=right x:num>'.date('Y-m-d H:i',$cv['maxtime']).'</td>' ;
                $html .= '<td width="200" align=right x:num>'.parseWorkTime($time_sum).'</td>' ;
                $html .= '<td width="100" align=right x:num></td>' ;
                $html .= '<td width="100" align=right x:num></td>' ;
                $html .= '</tr>' ;

                $time_start=$cv['mintime'];
            }else{
                $html .= '<tr>' ;
                $html .= '<td width="100" align=center x:str>'.($k+1).'</td>' ;
                $html .= '<td width="200" align=right x:num>'.date('Y-m-d H:i',$cv['mintime']).'</td>';
                $html .= '<td width="200" align=right x:num>'.date('Y-m-d H:i',$cv['maxtime']).'</td>' ;
                $html .= '<td width="200" align=right x:num>'.parseWorkTime($time_sum).'</td>' ;
                $html .= '<td width="100" align=right x:num></td>' ;
                $html .= '<td width="100" align=right x:num></td>' ;
                $html .= '</tr>' ;
            }

            //没有下一个时添加统计数据
            if(!$vo[$k+1]){
                $html .= '<tr>' ;
                $html .= '<td width="100" align=center x:str>合计(次)：'.($num-1).'</td>' ;
                $html .= '<td width="200" align=right x:num>'.date('Y-m-d H:i',$time_start).'</td>';
                $html .= '<td width="200" align=right x:num>'.date('Y-m-d H:i',$cv['maxtime']).'</td>' ;
                $html .= '<td width="100" align=right x:num>合计：'.parseWorkTime($time_per_sum).'</td>' ;
                $html .= '<td width="100" align=right x:num></td>' ;
                $html .= '<td width="100" align=right x:num></td>' ;
                $html .= '</tr>' ;
            }

            $k++;
        }
    }


    $html .= '</body></html></table>';
    header("Content-type:application/vnd.ms-excel");
    header("Content-Disposition:attachment;filename={$filename}.xls");
    echo $html ;
}



function parseWorkTime($time){
    $time_str='';
    if($time>3600){
        $h = floor($time/3600);
        $time = $time%3600;
        $time_str.=$h.'时';
    }
    if($time>60){
        $m = floor($time/60);
        $time = $time%60;
        $time_str.=$m.'分';
    }
    if($time)$time_str.=$time.'秒';

    return $time_str;
}
